using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api.Mobile
{
    /// <summary>
    /// YH_ScreeningDetail
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class YH_ScreeningDetailController : CoreApiController
    {
        private readonly YH_ScreeningDetailService _service;
        /// <summary>
        /// YH_ScreeningDetail
        ///</summary>
        public YH_ScreeningDetailController(YH_ScreeningDetailService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(YH_ScreeningDetailController) + "_" + nameof(YH_ScreeningDetailController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<YH_ScreeningDetailDto>
                pagingQueryModel)
        {
            IPageModel<YH_ScreeningDetailDto>
                result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {

                a.ReportType,//上报分类(0计划/1非计划)
                a.ScreeningPlanProDetailId,//排查计划项目明细ID
                a.InspectTime,//检查时间
                a.StandardID,//排查标准ID
                a.StandardContent,//排查标准内容
                a.Type,//标准/手动
                a.result,//排查结果
                a.StandingBookId,//隐患台账ID
                a.Status,//状态(保存/提交)
                a.ClearanceProjects,//排查项目
                a.AreaID,//区域
                a.LocationID,//位置
                a.PersonID,//排查人
                a.Time,//排查时间
                a.CreateFactory,//
                a.CreateDept,//
                a.UpdateDept,//
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(YH_ScreeningDetailController) + "_" + nameof(YH_ScreeningDetailController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(YH_ScreeningDetailDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(YH_ScreeningDetailController) + "_" + nameof(YH_ScreeningDetailController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.ReportType,//上报分类(0计划/1非计划)
                model.ScreeningPlanProDetailId,//排查计划项目明细ID
                model.InspectTime,//检查时间
                model.StandardID,//排查标准ID
                model.StandardContent,//排查标准内容
                model.Type,//标准/手动
                model.result,//排查结果
                model.StandingBookId,//隐患台账ID
                model.Status,//状态(保存/提交)
                model.ClearanceProjects,//排查项目
                model.AreaID,//区域
                model.LocationID,//位置
                model.PersonID,//排查人
                model.Time,//排查时间
                model.CreateFactory,//
                model.CreateDept,//
                model.UpdateDept,//
            };
            return this.JsonSuccess(retObj);
        }
        #endregion
    }
}
